Ext.define('DragonApp.view.monster.Controller', {
    extend: 'Ext.app.ViewController',

    alias: 'controller.monster',

    requires: [
        'DragonApp.util.Msg'
    ],

    onSearch: function(button, e, options) {

        //搜索字段
        var searchField = Ext.ComponentQuery.query('monstergrid')[0].queryById('search_field');

        //仓库过滤
        var store = Ext.data.StoreManager.lookup('jsonPmonsterStore');
        store.clearFilter();

        //模糊筛选
        store.filterBy(function(item) {
            return (item.get('monsterid').indexOf(searchField.getValue()) >= 0);
        });
    },

    onTextFieldSpecialKeySearch: function(field, e, options) {
        if (e.getKey() === e.ENTER) {
            this.onSearch();
        }
    },

    onTextFieldClear: function(field, newValue, oldValue, eOpts) {

        var store = Ext.data.StoreManager.lookup('jsonPmonsterStore');

        //清空复位
        if ('' == newValue.trim()) {
            store.clearFilter();
        }
    },
    //============
    //表单全空判断
    //============
    checkEmptyForm: function(form) {

        var fields = form.query('textfield');

        for (var i = 0; i < fields.length; i++) {
            var value = fields[i].getValue();
            if ("" != value && null != value) return false;
        }
        return true;
    },
    //============
    //佣兵信息查询
    //============
    onSubmitClick: function(button, e, options) {

        var me = this;

        var form = me.getView();

        // 字段全空判断

        if (this.checkEmptyForm(form)) {
            form.query('textfield')[0].focus();
            DragonApp.util.Msg.showToast('请输入正确的信息');
            return;
        }

        form = form.getForm();

        if (!form.isValid()) return;

        var formValue = form.getValues();

        var grid = Ext.ComponentQuery.query('monstergrid')[0];
        var store = grid.getStore();

        //数据仓库根据查询附加参数加载
        store.load({
            params: formValue,
            scope: this,
            callback: function(records, operation, success) {

                if (success == false) {
                    DragonApp.util.Msg.showToast('提示:数据加载请求失败');
                    store.removeAll();
                } else if (success == true) {
                    if ("user-not-exist" == operation._response.type) {
                        DragonApp.util.Msg.showToast(operation._response.msg);
                        store.removeAll();
                    }
                }
            }
        });
    },

    onTextFieldSpecialKey: function(field, e, options) {
        if (e.getKey() === e.ENTER) {
            if (this.getView().getForm().isValid()) {
                this.onSubmitClick();
            }
        }
    },

    //============
    //符文解锁渲染
    //============
    yesNoStatusRender: function(value) {
        if (0 == value) {
            return '<span style="color:red;"> &Chi; </span>';
        } else if (1 == value) {
            return '<span style="color:green;"> &radic; </span>';
        }
    }
});